////////////////////////////////////////
//	File Header
//	Author: Stephen Ringer | stephen.ringer@gmail.com
//	File: SAGARenderer.cpp
//
////////////////////////////////////////

////////////////////////////////////////
//	File Includes
#include "SAGARenderer.h"
//
////////////////////////////////////////

SAGARenderer::SAGARenderer(HINSTANCE hInst)
{
	m_hInst		= hInst;
	m_hDLL		= NULL;
	m_pDevice	= NULL;
}

SAGARenderer::~SAGARenderer()
{
	Release();
}

////////////////////////////////////////
//	Function Name: CreateDevice
//	Purpose:
//		To create an object that implements the interface
//	*Only Takes the string "Direct3D" for now
HRESULT SAGARenderer::CreateDevice(char *chAPI)
{
	char buffer[300];
	//	Checks which API is in use
	if(strcmp(chAPI, "Direct3D") == 0)
	{
		//	Loads the D3D DLL
		m_hDLL = LoadLibrary("SAGAD3D.dll");
		if(!m_hDLL)
		{
			MessageBox(	NULL,
						"Loading SAGAD3D.dll failed.",
						"SAGAEngine - Error", MB_OK | MB_ICONERROR);
			return E_FAIL;
		}
	}
	else
	{
		_snprintf_s(buffer, 300, "API '%s' not supported.", chAPI);
		MessageBox(	NULL, buffer, "SAGAEngine - Error",
					MB_OK | MB_ICONERROR);
		return E_FAIL;
	}

	CREATERENDERDEVICE _CreateRenderDevice = 0;
	HRESULT hr;
	//	Pointer to DLL function 'CreateRenderDevice'
	_CreateRenderDevice = (CREATERENDERDEVICE)GetProcAddress(m_hDLL, "CreateRenderDevice");
	//	Check if function was found
	if(!_CreateRenderDevice) return E_FAIL;
	//	Call DLL function to create the device
	hr = _CreateRenderDevice(m_hDLL, &m_pDevice);
	if(FAILED(hr))
	{
		MessageBox(	NULL, "CreateRenderDevice() from lib failed.",
					"SAGAEngine - Error", MB_OK | MB_ICONERROR);
		m_pDevice = NULL;
		return E_FAIL;
	}
	return S_OK;
}
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: Release
//	Purpose:
//		To destroy the Device object
void SAGARenderer::Release()
{
	RELEASERENDERDEVICE _ReleaseRenderDevice = 0;
	HRESULT hr;

	if(m_hDLL)
	{
		//	Pointer to dll function 'ReleaseRenderDevice'
		_ReleaseRenderDevice = (RELEASERENDERDEVICE)GetProcAddress(m_hDLL, "ReleaseRenderDevice");
	}
	//	Call DLL function to release the device
	if(m_pDevice)
	{
		hr = _ReleaseRenderDevice(&m_pDevice);
		if(FAILED(hr))
		{
			m_pDevice = NULL;
		}
	}
}
//
////////////////////////////////////////